# Author    : CoffeeChicken
# Date      : 2022/1/2 21:34
# Function  : 演示 绘制地图数据


from pyecharts import options as opts
from pyecharts.charts import Bar, Grid, Line, Liquid, Page, Pie
from pyecharts.commons.utils import JsCode
from pyecharts.components import Table
from pyecharts.faker import Faker
from pyecharts.globals import ThemeType
import requests
import json
from pyecharts import options as opts
from pyecharts.charts import Map
from pyecharts.globals import ThemeType

url = "https://y.dxy.cn/papi/hospital/detailInfo?hospital_id=166"

response = requests.get("http://192.168.134.161:8081/hospitals")

hospitals = json.loads(response.text)['hospitals']

# 获取各省市全部医院数据
all_hospital = {}

for hospital in hospitals:
    all_hospital[hospital['province']] = all_hospital.get(hospital['province'], 0) + 1
    # print(hospital)

all_hospital_keys = all_hospital.keys()
all_hospital_values = all_hospital.values()

# 获取各省市床位数
all_beds = {}
for hospital in hospitals:
    if hospital['bedNum'] != -1:
        all_beds[hospital['province']] = all_beds.get(hospital['province'], 0) + int(hospital['bedNum'])

# print(all_beds)
all_beds_keys = all_beds.keys()
all_beds_values = all_beds.values()

# 获取年住院人数
year_live_hospital = {}
for hospital in hospitals:
    if hospital['yearHospitalNum'] != "-1":
        year_live_hospital[hospital['province']] = year_live_hospital.get(hospital['province'], 0) + int(
            hospital['yearHospitalNum'])
year_live_hospital_keys = year_live_hospital.keys()
year_live_hospital_values = year_live_hospital.values()

# 获取年急诊人数
year_emergency_number = {}
for hospital in hospitals:
    if hospital['yearOutPatient'] != "-1":
        year_emergency_number[hospital['province']] = year_emergency_number.get(hospital['province'], 0) + int(
            hospital['yearOutPatient'])
year_emergency_number_keys = year_emergency_number.keys()
year_emergency_number_values = year_emergency_number.values()

# 获取年急诊人数
all_employee_number = {}
for hospital in hospitals:
    if hospital['yearOutPatient'] != "-1":
        all_employee_number[hospital['province']] = all_employee_number.get(hospital['province'], 0) + int(
            hospital['yearOutPatient'])

all_employee_number_keys = all_employee_number.keys()
all_employee_number_values = all_employee_number.values()


def all_hospital_charts() -> Map:
    c = (
        Map()
            .add("医疗数据", [list(z) for z in zip(all_hospital_keys, all_hospital_values)], "china")
            .set_global_opts(title_opts=opts.TitleOpts(title="各省三级与二级医院数量"),
                             visualmap_opts=opts.VisualMapOpts(max_=300))

    )
    return c


def all_beds_charts() -> Map:
    all_beds_chart = (
        Map(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
            .add("床位数量", [list(z) for z in zip(all_beds_keys, all_beds_values)], "china")
            .set_global_opts(title_opts=opts.TitleOpts(title="各省三级与二级医院床位数量"),
                             visualmap_opts=opts.VisualMapOpts(max_=230000))

    )
    return all_beds_chart


def year_live_hospital_charts() -> Map:
    year_live_hospital_chart = (
        Map(init_opts=opts.InitOpts(theme=ThemeType.WONDERLAND))
            .add("年住院人数", [list(z) for z in zip(year_live_hospital_keys, year_live_hospital_values)], "china")
            .set_global_opts(title_opts=opts.TitleOpts(title="各省三级与二级医院年住院人数"),
                             visualmap_opts=opts.VisualMapOpts(max_=3000000))

    )
    return year_live_hospital_chart


def year_emergency_charts() -> Map:
    year_emergency_number_chart = (
        Map(init_opts=opts.InitOpts(theme=ThemeType.WONDERLAND))
            .add("年急诊数", [list(z) for z in zip(year_emergency_number_keys, year_emergency_number_values)], "china")
            .set_global_opts(title_opts=opts.TitleOpts(title="各省三级与二级医院年急诊人数"),
                             visualmap_opts=opts.VisualMapOpts(max_=30000000))

    )
    return year_emergency_number_chart


def all_employee_charts() -> Map:
    year_employee_number_chart = (
        Map(init_opts=opts.InitOpts(theme=ThemeType.WONDERLAND))
            .add("员工总数", [list(z) for z in zip(all_employee_number_keys, all_employee_number_values)], "china")
            .set_global_opts(title_opts=opts.TitleOpts(title="各省医院员工总数"),
                             visualmap_opts=opts.VisualMapOpts(max_=30000000))

    )
    return year_employee_number_chart


def page_layout():
    page = Page(layout=Page.DraggablePageLayout)
    page.add(
        all_employee_charts(),
        all_beds_charts(),
        year_emergency_charts(),
        all_hospital_charts(),
        year_live_hospital_charts()
    )
    page.render("all_pages.html")


if __name__ == '__main__':
    page_layout()
